package com.jinlejia.erp.ui.house;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.jinlejia.erp.R;
import com.jinlejia.erp.bean.SearchRentCoreBean;
import com.jinlejia.erp.bean.SearchShsCoreBean;
import com.jinlejia.erp.databinding.FragmentSearchCoreHistoryBinding;
import com.jinlejia.erp.utils.DbUtil;
import com.jinlejia.erp.widget.TagCloudView;
import com.litesuits.orm.db.assit.QueryBuilder;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * 查询核心历史
 */
public class SearchCoreHistoryFragment extends Fragment {

    private String type;

    public SearchCoreHistoryFragment() {
        // Required empty public constructor
    }

    public static SearchCoreHistoryFragment newInstance(String serviceType) {
        SearchCoreHistoryFragment fragment = new SearchCoreHistoryFragment();
        Bundle args = new Bundle();
        args.putString("type", serviceType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString("type");
        }
    }

    private FragmentSearchCoreHistoryBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_search_core_history, container, false);
        binding = DataBindingUtil.bind(view);
        binding.btClear.setOnClickListener(this::onViewClicked);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = DataBindingUtil.bind(view);
        binding.btClear.setOnClickListener(this::onViewClicked);
        ArrayList<String> dataList = new ArrayList<>();
        final ArrayList<Object> data = new ArrayList<>();
        if (type.equals("shs")) {
            QueryBuilder<SearchShsCoreBean> shsQuery = new QueryBuilder<>(SearchShsCoreBean.class)
                    .appendOrderDescBy("id");
            ArrayList<SearchShsCoreBean> shsCoreBeans = DbUtil.getDB(getContext()).single().query(shsQuery);
            dataList.clear();
            data.clear();
            data.addAll(shsCoreBeans) ;
            for (Object o : data) {
                SearchShsCoreBean sscb = (SearchShsCoreBean) o;
                StringBuilder sb = new StringBuilder();
                if (!TextUtils.isEmpty(sscb.getEaste())){
                    sb.append(sscb.getEaste());
                }
                if (!TextUtils.isEmpty(sscb.getBuildingNo()))
                    sb.append(sscb.getBuildingNo());
                if (!TextUtils.isEmpty(sscb.getUnitNo()))
                    sb.append(sscb.getUnitNo());
                if (!TextUtils.isEmpty(sscb.getRoomNo()))
                    sb.append(sscb.getRoomNo());
                if (!TextUtils.isEmpty(sb.toString())){
                    dataList.add(sb.toString());
                }
            }

        } else if (type.equals("rent")) {
            QueryBuilder<SearchRentCoreBean> rentQuery = new QueryBuilder<>(SearchRentCoreBean.class)
                    .appendOrderDescBy("id");
            ArrayList<SearchRentCoreBean> shsCoreBeans = DbUtil.getDB(getContext()).single().query(rentQuery);
            dataList.clear();
            data.clear();
            data.addAll(shsCoreBeans) ;
            for (Object o : data) {
                SearchRentCoreBean sscb = (SearchRentCoreBean) o;
                StringBuilder sb = new StringBuilder();
                if (!TextUtils.isEmpty(sscb.getEaste())){
                    sb.append(sscb.getEaste());
                }
                if (!TextUtils.isEmpty(sscb.getBuildingNo()))
                    sb.append(sscb.getBuildingNo());
                if (!TextUtils.isEmpty(sscb.getUnitNo()))
                    sb.append(sscb.getUnitNo());
                if (!TextUtils.isEmpty(sscb.getRoomNo()))
                    sb.append(sscb.getRoomNo());
                if (!TextUtils.isEmpty(sb.toString())){
                    dataList.add(sb.toString());
                }
            }
        }
        binding.tagHistory.setTags(dataList);
        binding.tagHistory.setOnTagClickListener(new TagCloudView.OnTagClickListener() {
            @Override
            public void onTagClick(int position) {
                if( type.equals("rent") ){
                    SearchRentCoreBean srcb = (SearchRentCoreBean) data.get(position);
                    EventBus.getDefault().post(srcb,"TAG_SEARCH_RENT_HISTORY");
                }else if( type.equals("shs") ){
                    SearchShsCoreBean sscb = (SearchShsCoreBean) data.get(position);
                    EventBus.getDefault().post(sscb,"TAG_SEARCH_SHS_HISTORY");
                }
                FragmentActivity activity = getActivity();
                if (activity!=null){
                    activity.finish();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void onViewClicked(View view) {
        if (type.equals("shs")) {
            DbUtil.getDB(getContext()).single().delete(SearchShsCoreBean.class);
        } else if (type.equals("rent")) {
            DbUtil.getDB(getContext()).single().delete(SearchRentCoreBean.class);
        }
        binding.tagHistory.setTags(new ArrayList<>());
    }
}
